<?php

namespace App\Models\Collection;

class CollectionModel extends BaseCollectionModel
{
    protected $table = 'collection';

    const TYPE_MODEL   = 10; // 模型
    const TYPE_SPACE   = 20; // 空间
    const TYPE_PICTURE = 30; // 图片

    const SUB_TYPE_OBJ = 1; // obj类型
    const SUB_TYPE_GJB = 2; // glb类型

    /**
     * 列表总条数
     *
     * @return void
     */
    public function total(array $params)
    {
        return $this->_listWhereHandle($this, $params)
            ->count();
    }

    /**
     * 列表
     *
     * @param array $params
     * @param array $fields
     * @return mixed
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS)
    {
        $query = $this->select($fields);
        return $this->_listWhereHandle($query, $params)
            ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
            ->limit(PAGE_SIZE)
            ->get()
            ->toArray();
    }

    private function _listWhereHandle($query, $params)
    {
        if (!empty($params['title'])) {
            $query = $query->where('title', 'like', '%' . $params['title'] . '%');
        }
        if (!empty($params['type'])) {
            $query = $query->where('type', $params['type']);
        }
        if (!empty($params['subType'])) {
            $query = $query->where('sub_type', $params['subType']);
        }
        
        return $query->where('deleted', self::DELETED_NO);
    }
}
